<template>
  <div class="c-ph24" v-if="withdrawSettings">
    <div class="c-flex-row c-aligni-center c-pt30 c-pb34 c-bd-b1-f6">
      <div class="c-fs24 c-fw600 c-fc-xblack c-ww156">发票类型</div>
      <div class="c-fs24 c-fc-xblack c-w0 c-flex-grow1">{{ withdrawSettings.invoiceType == 2 ? '个人' : '公司' }}</div>
    </div>
    <template v-if="withdrawSettings.invoiceType == 1">
      <div class="c-flex-row c-aligni-center c-pt30 c-pb34 c-bd-b1-f6">
        <div class="c-fs24 c-fw600 c-fc-xblack c-ww156">公司税号</div>
        <div class="c-fs24 c-fc-xblack c-w0 c-flex-grow1">{{withdrawSettings.taxNumber}}</div>
        <div class="c-fs24 theme-fc" @click="clickCopyText">复制</div>
      </div>
      <div class="c-flex-row c-aligni-center c-pt30 c-pb34 c-bd-b1-f6">
        <div class="c-fs24 c-fw600 c-fc-xblack c-ww156">公司名称</div>
        <div class="c-fs24 c-fc-xblack c-w0 c-flex-grow1">{{withdrawSettings.companyName}}</div>
      </div>
    </template>
    <template v-if="withdrawSettings.invoiceType == 2">
      <div class="c-flex-row c-aligni-center c-pt30 c-pb34 c-bd-b1-f6">
        <div class="c-fs24 c-fw600 c-fc-xblack c-ww156">姓名</div>
        <div class="c-fs24 c-fc-xblack c-w0 c-flex-grow1">{{withdrawSettings.individualName}}</div>
        <div class="c-fs24 theme-fc" @click="clickCopyText">复制</div>
      </div>
      <div class="c-flex-row c-aligni-center c-pt30 c-pb34 c-bd-b1-f6">
        <div class="c-fs24 c-fw600 c-fc-xblack c-ww156">身份证号</div>
        <div class="c-fs24 c-fc-xblack c-w0 c-flex-grow1">{{withdrawSettings.identityCard}}</div>
      </div>
    </template>
    <div class="c-pt30 c-pb20">
      <div class="c-fs24 c-fw600 c-fc-xblack c-lh40 c-ww156">发票规则</div>
    </div>
    <div id="descParent" @click="clickDesc" class="tinymce-text c-wb-ba c-pt20 c-p c-pz1 aCloseBig" v-html="rulesContent"></div>
  </div>
</template>

<script>
import { wxUtilJs, utilJs } from "@/utils/common.js"
let previewTimmer = null;
export default {
  name: 'InvoiceRule',
  components: {
  },
  props: [],
  beforeCreate() {

  },
  created() {
    utilJs.appShare(this);
  },
  data() {
    return {
      withdrawSettings: null,
      rulesContent:''
    }
  },
  mounted() {

  },
  computed: {

  },
  watch: {
  },
  methods: {
    getWithdrawSettings() {
      utilJs.getMethod(`${global.apiurl}common/getWithdrawSettings`, res => {
        this.withdrawSettings = res;
        if (!this.rulesContent) {
          this.getRules();
        }
      })
    },
    clickCopyText() {
      let copyText = ''
      if (this.withdrawSettings.invoiceType == 1) {
        copyText = this.withdrawSettings.taxNumber + '\n' + this.withdrawSettings.companyName
      } else {
        copyText = this.withdrawSettings.individualName + '\n' + this.withdrawSettings.identityCard
      }
      utilJs.copyText(copyText);
    },
    getRules() {
      let getUrl = 'https://ck-bkt-kp2-sz.oss-cn-shenzhen.aliyuncs.com/descHtml/nkqe/5/2025-02-19/859a2e8b66b5bccc7445f97a61a18e83.html'
      utilJs.getPageHtml(getUrl, sres => {
        sres = this.replaceHref(sres);
        this.rulesContent = sres;
      });
    },
    replaceHref(txt) {
      // 主要用于小程序环境判断详情内容是否有对应的公众号链接，有则统一替换成小程序套壳的链接地址
      let regex = /(<a\s+[^>]*?)(href=["']?)([^"'>]+)(\/?"[^>]*?>)/g;
      txt = txt.replace(regex, function (match, p1, p2, p3, p4) {
        let newHref = p3;
        let companyId = localStorage.getItem('companyId');
        if (
            utilJs.checkWxApp() &&
            ((p3.indexOf('kptest.ckjr001.com/kpv2p') > -1) || (p3.indexOf('wx.ckjr001.com/kpv2p') > -1) || (p3.indexOf('wxapph5.ckjr001.com') > -1)) &&
            ((p3.indexOf('kpv2plive') > -1 ? p3.split('kpv2plive/')[1].split('/?#')[0] : p3.split('kpv2p/')[1].split('/?#')[0]) == companyId)
        ) {
          newHref = `https://wxapph5.ckjr001.com/mini/${p3.indexOf('kpv2plive') > -1 ? p3.split('kpv2plive/')[1] : p3.split('kpv2p/')[1]}`;
        }
        return p1 + p2 + newHref + p4;
      });
      console.log('替换后html: ' + txt);
      return txt
    },
    clickDesc(e) {
      let imgNodeList = document.querySelectorAll('#descParent img, #descImgParent img');
      let imgArr = [];
      for (let item of imgNodeList) {
        item.src && imgArr.push(item.src)
      }
      previewTimmer = setTimeout(() => {
        if (e.target.tagName == 'IMG' && e.target.src) {
          wxUtilJs.previewImage({
            current: e.target.src, // 当前显示图片的http链接
            urls: imgArr // 需要预览的图片http链接列表
          });
        }
      }, 200)
    },
  },
  activated() {
    setDocumentTitle("发票规则");// eslint-disable-line
    this.getWithdrawSettings();
  },
  deactivated() {
    clearTimeout(previewTimmer);
  }
}
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
</style>
